(function($) {
    $('.mui-scroll-wrapper').scroll({
        bounce: true
    });
})(mui)
$(function() {
    function InitData() {
        this.init()
    }

    InitData.prototype.init = function() {
        var _this = this;
        //获取列表 
        var data = {
            contractType: 'NEW_SIGN',
            leadStudentId: app.getParameterByName('leadStudentId'),
        }
        app.posttoken(app.url.api_base + 'contracts/prepare-create', data, function(req) {
            if (req.code == 0) {
                var data = req.data.contractPrepareResponse;
                $('#FiName').html(data.studentName)
                $('#FiName').attr('studentid', data.leadStudentId)
                $('#FiFiex').html(data.gender)
                $('#Fibrithday').html(data.birthDay)
                $('#FiAge').html(data.gradeName)
                $('#FiAge').attr('dataid',data.gradeId)
                $('#FiPhone').html(data.contractMobile)
                $('#FiPerson').html(data.contractName)
            }else{
                mui.alert(req.message,function(){
                    app.back();
                })
            }
        }, true)

        //获取购买课程
        // var data = {
        //     'leadStudentId': app.getParameterByName('leadStudentId'),
        // }
        app.posttoken(app.url.api_base + "selections/get-public-courses", {} , function(req) {
            if (req.code == 0) {
                if (req.data.response.length > 0) {
                    $('#FiCourse').html(req.data.response[0].text);
                    $('#FiCourse').attr('dataid', req.data.response[0].id)
                    $('#FiClass').html(req.data.response[0].lessonHour)
                    $('#FiCoursePrise').html(req.data.response[0].courseFee)
                    $('#FiJiaoPrise').html(req.data.response[0].otherFee)
                    $('#FiMouthPrise').html(req.data.response[0].validDate)
                    $('#FiTotalPrise').html(req.data.response[0].courseFee + req.data.response[0].otherFee)
                } else {
                    mui.alert('购买课程为空！', '提示');
                    return false;
                }
            }
        }, false)

        //关闭键盘
        $(document).on('tap','.simulation,.IisBirthday',function(){
            $('input').blur();
        })

        //获取购买课程
        // $(document).on('tap', '#FiCourse', function() {
        //     loadData()
        // })

        //提交生成合同
        $(document).on('tap', '.FiCopy', function() {
            var data = checkData();
            if (data == false) {
                return false;
            }
            app.posttoken(app.url.api_base + '/contracts/create', data, function(req) {
                if (req.code == 0) {
                    var id = req.data.contractId
                    if(id!=undefined){
                        app.go('../../index.html#/ArrayCourse/' + id);
                    }else{
                      mui.alert(req.message, '提示');
                      return false;  
                    }

                }else{
                     mui.alert(req.message, '提示');
                }
            }, true)
        })

        //设置年级
        // $(document).on('tap', '#FiAge', function() {
        //     var dthis = this;
        //     var picker1 = new mui.PopPicker();
        //     app.posttoken(app.url.api_base + "selections/grades", {}, function(req) {
        //         if (req.code == 0) {
        //             if (req.data.grades.length > 0) {
        //                 picker1.setData(req.data.grades);
        //                 picker1.show(function(selectItems) {
        //                     $(dthis).html(selectItems[0].text);
        //                     $(dthis).attr('dataid', selectItems[0].id);
        //                     $('#FiCourse').html('')
        //                     $('#FiClass').html('')
        //                 })
        //             } else {
        //                 mui.alert('年级数据为空！', '提示');
        //                 return false;
        //             }
        //         }
        //     }, false)
        // })

        //获取课程
        function loadData() {
            var dthis = $('#FiCourse');
            var picker1 = new mui.PopPicker();
            var gradeId= $('#FiAge').attr('dataid') == null ? '':$('#FiAge').attr('dataid');
            if(gradeId==''){
                mui.alert('请先选择年级', '提示');
                $('#FiAge').html('请选择年级')
                $('#FiAge').attr('dataid','');
                return false;
            }
            var data = {
               subjectType:'LITERATURE', 
               gradeId:gradeId
            }
            app.posttoken(app.url.api_base + "/selections/courses", data, function(req) {
                if (req.code == 0) {
                    if (req.data.response.length > 0) {
                        picker1.setData(req.data.response);
                        picker1.show(function(selectItems) {
                            $(dthis).html(selectItems[0].text);
                            $(dthis).attr('dataid', selectItems[0].id)
                            $('#FiClass').html(selectItems[0].lessonHour)
                            $('#FiCoursePrise').html(selectItems[0].courseFee)
                            $('#FiJiaoPrise').html(selectItems[0].otherFee)
                            $('#FiMouthPrise').html(selectItems[0].validDate)
                            $('#FiTotalPrise').html(selectItems[0].courseFee + selectItems[0].otherFee)
                        })
                    } else {
                        mui.alert('购买课程为空！', '提示');
                        return false;
                    }
                }
            }, false)
        }

        //数据校验
        function checkData() {
            var FiName = $('#FiName').html();
            // var FiFiex = $('#FiFiex').html();
            var Fibrithday = $('#Fibrithday').html();
            var FiAge = $('#FiAge').attr('dataid') == null ? '':$('#FiAge').attr('dataid');
            var FiPhone = $('#FiPhone').html();
            var FiPerson = $('#FiPerson').html();

            // var FiCourse = $('#FiCourse').html();
            var FiCourse = $('#FiCourse').attr('dataid') == null ? '':$('#FiCourse').attr('dataid');
            var FiClass = $('#FiClass').html();
            var FiCoursePrise = $('#FiCoursePrise').html();
            var FiTotalPrise = $('#FiTotalPrise').html();
            var FiJiaoPrise = $('#FiJiaoPrise').html();
            var FiMouthPrise = $('#FiMouthPrise').html();
            var FixianPrise = $('#FixianPrise').val().trim();
            var FiweixinPrise = $('#FiweixinPrise').val().trim();
            var FiposPrise = $('#FiposPrise').val().trim();
            var FizhifuPrise = $('#FizhifuPrise').val().trim();
            var otherPrice = $('#otherPrice').val().trim();
            var contractNo=$('#contractNo').val().trim();
            if(Number(FixianPrise)<=0){
                FixianPrise=0;
            }
            if(Number(FiweixinPrise)<=0){
                FiweixinPrise=0;
            }
            if(Number(FiposPrise)<=0){
                FiposPrise=0;
            }
            if(Number(FizhifuPrise)<=0){
                FizhifuPrise=0;
            }
            if(Number(otherPrice)<=0){
                otherPrice=0;
            }
            var Allprice = Number(FixianPrise)+ Number(FiweixinPrise) + Number(FiposPrise) + Number(FizhifuPrise) + Number(otherPrice);
            var total = Number(FiTotalPrise);
            console.log(Allprice)
            console.log(total);
            // if(FiAge==''){
            //     mui.alert('年级不能为空！', '提示');
            //     return false;
            // }
            if(contractNo==''){
                mui.alert('合同编号不能为空！', '提示');
                return false;
            }
            if (FiCourse == '') {
                mui.alert('购买课程不能为空！', '提示');
                return false;
            };
            if (Allprice != total) {
                mui.alert('支付费用与总金额不等！', '提示');
                return false;
            };
            var data = {
                leadStudentId: $('#FiName').attr('studentid'),
                // gradeId:FiAge,
                courseId: $('#FiCourse').attr('dataid'),
                contractNo:contractNo,
                totalPrice: FiTotalPrise,
                otherPrice: otherPrice,
                cashPrice: FixianPrise,
                posPrice: FiposPrise,
                zfbPrice: FizhifuPrise,
                wechatPrice: FiweixinPrise,
                contractType: 'NEW_SIGN',
                contractCategory: 'PUBLIC'
            }
            return data;
        }

    }



    new InitData();
})